﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Sbook.Data
{
    public class CategoryInfoController : SqlDataProvider
    {
        #region[CategoryInfo_GetById]
        public List<CategoryInfo> Category_GetById(string Id)
        {
            List<Data.CategoryInfo> list = new List<Data.CategoryInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_GetById", GetConnection()))
            {
                Data.CategoryInfo obj = new Data.CategoryInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@cID", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.CategoryIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[CategoryInfo_GetByTop]
        public List<CategoryInfo> Category_GetByTop(string Top, string Where, string Order)
        {
            List<Data.CategoryInfo> list = new List<Data.CategoryInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_GetByTop", GetConnection()))
            {
                Data.CategoryInfo obj = new Data.CategoryInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.CategoryIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[CategoryInfo_GetByAll]
        public List<CategoryInfo> Category_GetByAll()
        {
            List<Data.CategoryInfo> list = new List<Data.CategoryInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_GetByAll", GetConnection()))
            {
                Data.CategoryInfo obj = new Data.CategoryInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.CategoryIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[CategoryInfo_Paging]
        public List<CategoryInfo> Category_Paging(string CurentPage, string PageSize)
        {
            List<Data.CategoryInfo> list = new List<Data.CategoryInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_Paging", GetConnection()))
            {
                Data.CategoryInfo obj = new Data.CategoryInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.CategoryIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[CategoryInfo_Insert]
        public bool Category_Insert(CategoryInfo data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@cName", data.cName));
                dbCmd.Parameters.Add(new SqlParameter("@cStatus", data.cStatus));
                dbCmd.Parameters.Add(new SqlParameter("@cpID", data.cpID));
                dbCmd.Parameters.Add(new SqlParameter("@cCode", data.cCode));

                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Category");
            return true;
        }
        #endregion
        #region[CategoryInfo_Update]
        public bool Category_Update(CategoryInfo data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@cID", data.cID));
                dbCmd.Parameters.Add(new SqlParameter("@cName", data.cName));
                dbCmd.Parameters.Add(new SqlParameter("@cStatus", data.cStatus));
                dbCmd.Parameters.Add(new SqlParameter("@cpID", data.cpID));
              
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Category");
            return true;
        }
        #endregion
        #region[Category_Delete]
        public bool Category_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Category_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@cID", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Category");
            return true;
        }
        #endregion
    }
}